/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.cells;

import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.layout.VBox;

import java.util.Objects;

/**
 * 是否适用指标可编辑单元格
 * @author zpy
 * @version 1.0.0
 */
public class ValidResultTableCell<T> extends TableCell<T,Boolean> {
    protected final static String FAILED_TABLE_ROW_STYLE = "failed-table-row";

    private final String successText;
    private final String failedText;

    public ValidResultTableCell(String successText,String failedText){
        this.successText = successText;
        this.failedText = failedText;
    }

    @Override
    public void updateItem(Boolean item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setText(null);
            setGraphic(null);
        }else {
            boolean checkResult = Objects.nonNull(item) && item;
            String content = checkResult? successText : failedText;
            VBox vBox = new VBox();
            vBox.getStyleClass().add("common-table-cell-box");
            vBox.getChildren().add(new Label(content));
            setGraphic(vBox);
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            if(Objects.nonNull(getTableRow()) && Objects.nonNull(getTableRow().getStyleClass())){
                getTableRow().getStyleClass().removeAll(FAILED_TABLE_ROW_STYLE);
                if(!checkResult){
                    getTableRow().getStyleClass().add(FAILED_TABLE_ROW_STYLE);
                }
            }
        }
    }
}